var Mock = require("mockjs");
const patient = ['健康', '冠心病', '高血压', '气管炎', '偏头痛', '感冒', '支气管扩张', '肺炎'];
var Random = Mock.Random;

const guangdongPlaces = [
  "荔湾区",
  "越秀区",
  "海珠区",
  "天河区",
  "白云区",
  "黄埔区",
  "番禺区",
  "花都区",
  "南沙区",
  "增城区",
  "从化区",
];
const Live_direction = ['Java程序员', '前端工程师', '产品经理', '测试工程师', '运维工程师', 'UI设计师', '大数据工程师', '网络安全工程师', '数据库工程师', '网络工程师', '网络安全工程师']
const School_of_Tourism = [
  '建筑工程学院',
  '艺术与设计学院',
  '经济与管理学院',
  '机电工程学院',
  '信息工程学院',
  '外语外贸学院',
  '人文学院',
  '创业教育学院',
  '继续教育学院',
  '体育健康学院',
  '马克思主义学院'
]

const company_list = [
  "广州超越按揭服务有限公司",
  "广州华腾普惠代办按揭服务有限公司",
  "小行家按揭（广州）信息咨询有限公司",
  "广州天河区津滨腾越大厦南塔2103",
];
let users = []
const count = 20
const tokens = []
for (let i = 0; i < count; i++) {
  users.push(
    Mock.mock({
      id: i,
      ID: 'token' + i + Mock.Random.guid().slice(0, 13),
      name: Mock.Random.cname(),
      newname: Mock.Random.cname(),
      nickName: '@last',
      title: Mock.Random.cname(),
      age: '20230901' + Random.integer(20, 50),
      sex: Mock.Random.integer(0, 1),
      addr: '广州市 ' + guangdongPlaces[Math.floor(Math.random() * guangdongPlaces.length)],
      Live_direction1: Live_direction[Math.floor(Math.random() * Live_direction.length)],
      School_of_Tourism1: School_of_Tourism[Math.floor(Math.random() * School_of_Tourism.length)],
      company_list1: company_list[Math.floor(Math.random() * company_list.length)],
      // addr: `${Random.province()}-${Random.city()}-${Random.county()}`,

      birth: Random.datetime("yyyy-MM-dd"),  // 值是指定格式的日期字符串
      birth2: '2023-' + Random.datetime("MM-dd"),  // 值是指定格式的日期字符串
      birth3: '2023-' + Random.datetime("MM-dd"),  // 值是指定格式的日期字符串
      confirm: Math.floor(Math.random() * 100 + 1),
      patientindex: patient[Math.floor(Math.random() * patient.length)],
      'age1|1-3': 1,
    })
  )
  // console.log(users[1]);
  tokens.push(users[i].ID)
}
let user1 = toObject(tokens, users);
function toObject(names, values) {
  var result = {};
  for (var i = 1; i < names.length; i++)
    result[names[i]] = values[i];
  return result;
}
// console.log(user1);

export default {
  login: (config) => {
    const { username } = JSON.parse(config.body);
    const token = tokens[username];
    if (!token) {
      return {
        status: 1,
        message: "用户名或密码错误",
      };
    }
    return {
      status: 0,
      token,
    };
  },
  userInfo: (config) => {
    const token = config.body;
    const userInfo = user1[token];
    if (!userInfo) {
      return {
        status: 1,
        message: "获取用户信息失败",
      };
    }
    return {
      status: 0,
      userInfo,
    };
  },
  getUsers: () => {
    return {
      status: 0,
      users: Object.values(user1),
    };
  },
  deleteUser: (config) => {
    const { id } = JSON.parse(config.body);
    // console.log(id);
    const token = tokens[id];
    // console.log(tokens[id]);
    // console.log(users);
    if (token) {
      delete user1[token]
      delete tokens[id];
    }
    return {
      status: 0,
    };
  },
  editUser: (config) => {
    const data = JSON.parse(config.body);
    const { id } = data;
    const token = tokens[id];
    if (token) {
      user1[token] = { ...user1[token], ...data };
    }
    return {
      status: 0,
    };
  },
  ValidatUserID: (config) => {
    const userID = config.body;
    const token = tokens[userID];
    if (token) {
      return {
        status: 1,
      };
    } else {
      return {
        status: 0
      };
    }
  },
  addUser: (config) => {
    const data = JSON.parse(config.body);
    const { id } = data;
    tokens[id] = `${id}-token`
    user1[`${id}-token`] = {
      ...user1[`${id}-token`],
      ...data
    }
    return {
      status: 0,
    };
  },
  logout: (_) => {
    return {
      status: 0,
      data: "success",
    };
  },
};
